package com.mhc.suzuki.controller;

import com.mhc.haval.security.model.User;
import com.mhc.suzuki.api.model.DealerAccountDTO;
import com.mhc.suzuki.api.model.DealerDetailDTO;
import com.mhc.suzuki.api.model.DealerInfoDTO;
import com.mhc.suzuki.api.model.DealerQueryDTO;
import com.mhc.suzuki.api.service.IDealerService;
import com.mhc.suzuki.constant.ErrorCodeEnum;
import com.mhc.suzuki.vo.DealerAccountVO;
import com.mhc.suzuki.vo.DealerDetailVO;
import com.mhc.suzuki.vo.DealerInfoVO;
import com.subaru.common.entity.BizResult;
import com.subaru.common.util.BeanCopierUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * Created by HW
 * on 2017/9/14 21:27.
 * 经销商管理接口
 */

@Slf4j
@RestController
@RequestMapping(value = "/dealer")
public class DealerController {


    @Autowired
    private IDealerService iDealerService;


    /**
     * 所有经销商列表获取
     * @param user
     * @return
     */
    @RequestMapping(value = {"/lists/{keyword}"}, method = RequestMethod.GET)
    public BizResult<List<DealerInfoVO>> getDealerList(@AuthenticationPrincipal User user, @PathVariable String keyword) {

        Optional<User> userOptional = Optional.ofNullable(user);
        if (userOptional.isPresent()) {

            DealerQueryDTO dealerQueryBO = new DealerQueryDTO();
            dealerQueryBO.setCompanyNo(user.getCompanyNo());

            //查询经销商列表
            List<DealerInfoDTO> dealerInfoDTOS = iDealerService.getAllDealerInfoDOS(dealerQueryBO, keyword.replace("undefined",""));

                /*BO->VO*/
            List<DealerInfoVO> dealerInfoVOS = new ArrayList<>();
            for (DealerInfoDTO dealerInfoDTO : dealerInfoDTOS) {
                DealerInfoVO dealerInfoVO = BeanCopierUtil.convert(dealerInfoDTO, DealerInfoVO.class);
                dealerInfoVOS.add(dealerInfoVO);
            }

            return BizResult.create(dealerInfoVOS, true, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());

        } else {
            return BizResult.create(ErrorCodeEnum.NOT_LOGIN.getErrCode(), ErrorCodeEnum.NOT_LOGIN.getErrMsg());
        }

    }




    /**
     * 根据经销商id查询经销商具体信息
     * @param dealerId
     * @return
     */
    @ApiOperation(value = "经销商详细信息查看", notes = "获取经销商详细信息")
    @RequestMapping(value = {"/detail/{dealerId}"}, method = RequestMethod.GET)
    public BizResult<DealerDetailVO> getDealerDetail(@PathVariable Long dealerId){
        //查询操作
        DealerDetailDTO dealerDetailBO = iDealerService.getDealerDetail(dealerId);
        DealerDetailVO dealerDetailVO = BeanCopierUtil.convert(dealerDetailBO, DealerDetailVO.class);
        List<DealerAccountVO> dealerAccountVOS = new ArrayList<>();
        for(DealerAccountDTO dealerAccountBO : dealerDetailBO.getDealerAccountDTOS()){
            DealerAccountVO dealerAccountVO = BeanCopierUtil.convert(dealerAccountBO, DealerAccountVO.class);
            dealerAccountVOS.add(dealerAccountVO);
        }
        dealerDetailVO.setDealerAccountVOS(dealerAccountVOS);
        return BizResult.create(dealerDetailVO, true, ErrorCodeEnum.SUCCESS.getErrCode(), ErrorCodeEnum.SUCCESS.getErrMsg());

    }



}
